from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import List
from myapp.models import Lingo


def select(request):
    if request.method == 'POST':
        try:
            username = request.COOKIES['username'].encode('iso-8859-1').decode('utf-8')
            one = Lingo.objects.get(username__exact=username)
        except KeyError:
            return render(request, 'login.html', {"error_message": '请先登录再进行投票'})
        if one.number_of_times == 0:
            selected_item = request.POST['item']
            item = List.objects.get(id__exact=selected_item)
            if item.name == username:
                items = List.objects.all()
                context = {
                    "username": username,
                    'items': items,
                    'number_of_times': False,
                    "error_message": '不允许给自己投票',
                }
                return render(request, 'campaign-for.html', context=context)
            else:
                one.number_of_times += 1
                one.save()
                item.number += 1
                item.save()
                items = List.objects.all()
                context = {
                    "username": username,
                    'items': items,
                    'number_of_times': True
                }
                return render(request, 'campaign-for.html', context=context)
        else:
            items = List.objects.all()
            context = {
                "username": username,
                'items': items,
                'number_of_times': True,
                "error_message": '你已经投票了'
            }
            return render(request, 'campaign-for.html', context=context)
    else:
        items = List.objects.all()
        try:
            username = request.COOKIES['username'].encode('iso-8859-1').decode('utf-8')
            one = Lingo.objects.get(username__exact=username).number_of_times
        except KeyError:
            username = None
            one = 0
        if one == 0:
            number_of_times = False
        else:
            number_of_times = True
        context = {
            "username": username,
            "items": items,
            "number_of_times": number_of_times,
        }
        return render(request, 'campaign-for.html', context=context)


def important(request):
    if request.method == 'POST':
        username = request.COOKIES['username'].encode('iso-8859-1').decode('utf-8')
        try:
            participate = List.objects.get(name__exact=username)
        except:
            participate = False
        if participate:
            context = {
                'username': username,
                "error_message": '你已经提交竞选名单了，如需修改撤回请联系部长',
            }
            return render(request, 'important.html', context=context)
        else:
            position = request.POST['position']
            participate = List(
                name=username,
                position=position,
                number=0
            )
            participate.save()
            context = {
                'username': username,
                "error_message": '你成功提交了竞选名单，如需修改撤回请联系部长',
            }
            return render(request, 'important.html', context=context)

    else:
        try:
            username = request.COOKIES['username'].encode('iso-8859-1').decode('utf-8')
        except KeyError:
            return render(request, 'login.html', {"error_message": '请先登录再进行竞选'})
        context = {
            'username': username,
        }
        return render(request, 'important.html', context=context)
